package com.grandbroad.eim.utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;


import com.grandbroad.eim.app.MyApplication;
import com.grandbroad.eim.ui.activities.VideoActivity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 *
 * Created by even alone on 2018/6/2.
 */

public class UiUtlis {
    private UiUtlis()
    {

    }

    private static Context getContext()
    {
        return MyApplication.getContext();
    }


    /**
     * 共享元素的activity跳转
     * 使用前请确认做好了共享控件元素的配置
     * @param now
     * @param target
     * @param tag
     */
    public static void startAnimActivity(AppCompatActivity now,Class target,View taget_view,String tag,boolean flag)
    {
        Intent in = new Intent(now,target);
        if (flag)
        {
            ActivityOptionsCompat optionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation(now,taget_view,tag);
            now.startActivity(in,optionsCompat.toBundle());
        }
        else
        {
            now.startActivity(in);
        }

    }
    /**
     * 共享元素的activity跳转
     * 绑定了bundle
     * @param now 当前
     * @param target 目标
     * @param tag view的tag
     * @param key 关键字
     * @param o 传递的实体
     */
    public static void startAnimActivity(AppCompatActivity now,Class target,View taget_view ,String tag,String key,Object o,boolean flag)
    {
        Bundle bundle = new Bundle();
        bundle.putSerializable(key, (Serializable) o);
        Intent in = new Intent(now,target);
        in.putExtras(bundle);
        if (flag)
        {
            ActivityOptionsCompat optionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation(now,taget_view,tag);
            now.startActivity(in,optionsCompat.toBundle());
        }
        else
        {
            now.startActivity(in);
        }


    }

    public static List<String> getViewPagerTitle(@IdRes int id)
    {
        @SuppressLint("ResourceType") String[] rs = getContext().getResources().getStringArray(id);
        List<String> list = new ArrayList<>();
        for (String s :rs)
        {
            list.add(s);
        }
        return list;
    }

    /**
     * 获取本地版本号
     * @return 版本号
     */
    public static String getVersion()
    {
        String version ="";

        PackageManager manager = getContext().getPackageManager();
        try
        {
            PackageInfo info = manager.getPackageInfo(getContext().getPackageName(),0);
            version = info.versionName;
            return version;
        }
        catch (PackageManager.NameNotFoundException e)
        {
            e.printStackTrace();
        }
        return null;

    }
    /**
     * 取得颜色
     * @param colorid 颜色id
     * @return
     */
    public static int getColor(int colorid)
    {
        return getContext().getResources().getColor(colorid);
    }

    /***
     * 获取资源id
     * @param id
     * @return
     */
    @SuppressLint("ResourceType")
    public static Drawable getDrawable(@IdRes int id)
    {
        return getContext().getResources().getDrawable(id);
    }

    /**
     * 获取view
     * @param viewid 视图id
     * @return
     */
    public static View getView(int viewid)
    {
        return View.inflate(getContext(),viewid,null);
    }

    /**
     * 根据 url 获取 host name
     * http://www.gcssloop.com/ => www.gcssloop.com
     */
    public static String getHost(String url) {
        if (url == null || url.trim().equals("")) {
            return "";
        }
        String host = "";
        Pattern p = Pattern.compile("(?<=//|)((\\w)+\\.)+\\w+");
        Matcher matcher = p.matcher(url);
        if (matcher.find()) {
            host = matcher.group();
        }
        return host;
    }

    /**
     * 获取随机颜色
     * @return
     **
    public static int RandomColor()
    {

        int[] customizedColors = getContext().getResources().getIntArray(R.array.customizedColors);
        int customizedColor = customizedColors[new Random().nextInt(customizedColors.length)];
        return customizedColor;
    }
    */

}